package com.yx.changdao.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Pager;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.entity.*;
import com.yx.changdao.common.enums.ApplyType;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.*;
import com.yx.changdao.common.vo.ApplyProblemVO;
import com.yx.changdao.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 问题上报审批; 申请审批;
 *
 * @Auther: xiapeixin
 * @create 2020/04/03 16:51
 */
@RestController
@RequestMapping("/problem/verify")
public class ProblemVerifyController {

    /**
     * 问题服务
     */
    @Autowired
    private ProblemService problemService;

    @Autowired
    private ProblemBeatbackService beatbackService;
    /**
     * 问题信息(在分配之前)服务
     */
    @Autowired
    private ProblemInfoService problemInfoService;

    @Autowired
    private GridInfoService gridInfoService;

    /**
     * 上报审批------------------------------------------------------------------------------------------------
     */
    /**
     * 列表
     *
     * @return Result
     */
    @AutoLog
    @GetMapping("/report/list")
    @RequiresPermissions("problem:report:list")
    @ShiroDesc("问题上报审批:列表")
    public Result listReport(@RequestParam(value = "page") Long page,
                             @RequestParam(value = "limit", required = false) Integer limit) {
        if (limit == null) {
            limit = SysConst.LIMIT_PAGE;
        }

        // 分页配置
        Page<ProblemInfo> pager = new Page<>(page, limit);
        // 根据请求解析条件。
        QueryWrapper<ProblemInfo> qw = SysUtils.autoCnd(ProblemInfo.class);
        qw.orderByDesc(ProblemInfo.CREATE_TIME); //时间倒序
        qw.eq(ProblemInfo.IF_REPORT, 1); //上报待审核的;

        // 获取分页结果集
        Page<ProblemInfo> pageinfo = problemInfoService.page(pager, qw);

        //张从阵 脱敏
        pageinfo.getRecords().forEach(p -> {
            p.setResidentIdcard(DesensitizationUtils.idEncrypt(p.getResidentIdcard()));
            GridInfo gridInfo = gridInfoService.getById(p.getGridId());
            if (null != gridInfo) {
                p.setGridId(Integer.valueOf(gridInfo.getGridNum()));
            }
        });
        Map<String, Object> map = new HashMap<>();
        map.put("list", pageinfo.getRecords());
        map.put("pager", new Pager(pageinfo));
        return Result.ok().data(map);
    }

    /**
     * 上报打回记录列表
     */
    @AutoLog
    @GetMapping("/beatback/list")
    public Result beatReport(@RequestParam(value = "page") Long page,
                             @RequestParam(value = "limit", required = false) Integer limit,
                             @RequestParam(value = "proId") Integer proId) {
        if (limit == null) {
            limit = SysConst.LIMIT_PAGE;
        }

        // 分页配置
        Page<ProblemBeatback> pager = new Page<>(page, limit);
        // 根据请求解析条件。
        QueryWrapper<ProblemBeatback> qw = SysUtils.autoCnd(ProblemBeatback.class);

        qw.orderByDesc(ProblemBeatback.CREATE_TIME); //时间倒序

        qw.eq(ProblemBeatback.PROBLEM_INFO_ID, proId).orderByDesc(ProblemBeatback.CREATE_TIME);

        Page<ProblemBeatback> page1 = beatbackService.page(pager, qw);

        page1.getRecords().forEach(t -> {
            if (t.getReportStatus().equals(2)) {
                t.setReportCon("通过");
            } else if (t.getReportStatus().equals(3)) {
                t.setReportCon("打回");
            }
        });

        Map<String, Object> map = new HashMap<>();
        map.put("list", page1.getRecords());
        map.put("pager", new Pager(page1));

        return Result.ok().data(map);
    }

    /**
     * 驳回上报
     */
    @AutoLog
    @PutMapping("/report/reject")
    /*@RequiresPermissions("problem:report:reject")
    @ShiroDesc("问题上报审批:打回")*/
    public Result reject(@RequestParam("id") Integer id,
                         @RequestParam("content") String content) throws AlertException {

        if (!problemInfoService.rejectOrAgreeBatch(Faster.toList(id), content, 3)) {
            throw new AlertException("操作失败");
        }
        return Result.ok("操作成功");
    }

    /**
     * 上报通过
     */
    @AutoLog
    @PutMapping("/report/agree")
    /*@RequiresPermissions("problem:report:agree")
    @ShiroDesc("问题上报审批:通过")*/
    public Result agree(@RequestParam("id") Integer id) throws AlertException {
        if (!problemInfoService.rejectOrAgreeBatch(Faster.toList(id), "通过", 2)) {
            throw new AlertException("操作失败");
        }
        return Result.ok("操作成功");
    }

    /**
     * 批量同意上报
     */
    @AutoLog
    @PutMapping("/report/batch/agree")
    /*@RequiresPermissions("problem:report:agree:batch")
    @ShiroDesc("问题上报审批:批量通过")*/
    public Result agreeBatch(@RequestParam("ids") String ids) throws AlertException {
        if (!problemInfoService.rejectOrAgreeBatch(Faster.batch2(ids), "通过", 2)) {
            throw new AlertException("操作失败");
        }
        return Result.ok("操作成功");
    }

    /**
     * 申请审批------------------------------------------------------------------------------------------------
     */

    /**
     * 问题申请记录服务
     */
    @Autowired
    private ProblemApplyService problemApplyService;

    /**
     * 总控申请审批 疑难件 列表
     */
    @AutoLog
    @GetMapping("/apply/list")
    @RequiresPermissions("problem:apply:hard:list")
    @ShiroDesc("问题申请:疑难件列表")
    public Result applyList(@RequestParam(value = "page") Long page,
                            @RequestParam(value = "limit", required = false) Integer limit) {
        if (limit == null) {
            limit = SysConst.LIMIT_PAGE;
        }

        // 分页配置
        Page<ApplyProblemVO> pager = new Page<>(page, limit);
        // 根据请求解析条件。
        QueryWrapper<ApplyProblemVO> qw = SysUtils.autoCnd(ApplyProblemVO.class);

        qw.orderByDesc("create_time"); //时间倒序

        // 获取分页结果集。
        IPage<ApplyProblemVO> pageNex = problemApplyService.pageNex(pager, qw);

        pageNex.getRecords().forEach(p ->
                p.setResidentIdcard(DesensitizationUtils.idEncrypt(p.getResidentIdcard())));

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageNex.getRecords());
        map.put("pager", new Pager(pageNex));

        return Result.ok().data(map);
    }

    /**
     * 总控申请审批 疑难件 查看
     */
    @AutoLog
    @GetMapping("/apply")
    public Result apply(@RequestParam("applyId") Integer applyId) {
        ApplyProblemVO problemVO = problemApplyService.byId(applyId);
        problemVO.setResidentIdcard(DesensitizationUtils.idEncrypt(problemVO.getResidentIdcard()));
        return Result.ok().data(problemVO);
    }


    /**
     * 总控申请审批 疑难件 同意
     */
    @AutoLog
    @PutMapping("/apply/agree")
    /*@RequiresPermissions("problem.apply:agree")
    @ShiroDesc("问题申请:同意申请")*/
    public Result agreeApply(@RequestBody Map param) throws AlertException {

        Integer applyId = (Integer) param.get("applyId");
        List<Integer> depIds = (List<Integer>) param.get("depIds");

        if (!problemApplyService.agree(applyId, depIds)) {
            throw new AlertException("操作失败");
        }
        return Result.ok("操作成功");
    }


    /**
     * 总控申请审批 疑难件 批量同意
     */
    @AutoLog
    @PutMapping("/apply/batch/agree")
    /*@RequiresPermissions("problem.apply:agree:batch")
    @ShiroDesc("问题申请:批量同意申请")*/
    public Result agreeApplyBatch(@RequestBody Map param) throws AlertException {

        List<Integer> applyIds = (List<Integer>) param.get("applyIds");
        List<Integer> depIds = (List<Integer>) param.get("depIds");

        if (!problemApplyService.agreeBatch(applyIds, depIds)) {
            throw new AlertException("操作失败");
        }
        return Result.ok("操作成功");
    }


    /**
     * 总控申请审批 疑难件 拒绝
     */
    @AutoLog
    @PutMapping("/apply/reject")
    /*@RequiresPermissions("problem.apply:reject")
    @ShiroDesc("问题申请:拒绝申请")*/
    public Result rejectApply(@RequestParam("applyId") Integer applyId) throws AlertException {

        if (!problemApplyService.reject(applyId)) {
            throw new AlertException("操作失败");
        }
        return Result.ok("操作成功");
    }


    /**
     * 总控申请审批 疑难件 批量拒绝
     */
    @AutoLog
    @PutMapping("/apply/batch/reject")
    /*@RequiresPermissions("problem.apply:reject:batch")
    @ShiroDesc("问题申请:批量拒绝申请")*/
    public Result rejectApplyBatch(@RequestParam("applyIds") String applyIds) throws AlertException {

        if (!problemApplyService.rejectBatch(Faster.batch2(applyIds))) {
            throw new AlertException("操作失败");
        }
        return Result.ok("操作成功");
    }

}
